package zhoug.zui.valuepair;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;

import zhoug.common.filters.PhoneFormat;
import zhoug.common.utils.StringUtils;
import zhoug.zui.R;

import androidx.annotation.Nullable;

/**
 * 键值对水平布局组件:输入框
 *
 * @Author HK-LJJ
 * @Date 2020/3/23
 * @Description
 */
public class ZUIKeyValueEditText extends ZUIAbsKeyValueView<EditText> {
    private EditText editText;
    private boolean isPhone334;
    private Phone334TextWatcher mPhone334TextWatcher;

    public ZUIKeyValueEditText(Context context) {
        this(context, null);
    }

    public ZUIKeyValueEditText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZUIKeyValueEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void parseAttrs(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super.parseAttrs(context, attrs, defStyleAttr, defStyleRes);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZUIKeyValueEditText, defStyleAttr, defStyleRes);
        isPhone334 = typedArray.getBoolean(R.styleable.ZUIKeyValueEditText_zui_isPhone334, false);
    }

    @Override
    public EditText getValueView() {
        if (valueView == null) {
//            return new EditText(getContext());
            ViewGroup root = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.zui_keyvalue_edittext, null);
            editText = root.findViewById(R.id.editText);
            root.removeView(editText);
            setPhone334(isPhone334);
            return editText;
        }
        return valueView;
    }

    @Override
    public String getValue() {
        if (valueView != null) {
            String s = valueView.getText().toString();
            if (isPhone334) {
                return PhoneFormat.getPhoneNumber(s);
            }
            return StringUtils.isNullOrEmpty(s) ? null : s;
        }
        return null;
    }

    @Override
    protected void _setStatusColor(int colorStatus) {
        if (valueView != null) {
            valueView.setTextColor(colorStatus);
        }
    }

    public EditText getEditText() {
        return editText;
    }

    @Override
    public void setValue(CharSequence value) {
        if (valueView != null) {
            valueView.setText(value);
        }
    }

    @Override
    public void setError(CharSequence error) {
        super.setError(error);
//        editText.setError(error);
    }

    public void setPhone334(boolean phone334) {
        this.isPhone334 = phone334;
        if (editText != null) {
            if (mPhone334TextWatcher != null) {
                editText.removeTextChangedListener(mPhone334TextWatcher);
            }
            if (isPhone334) {
                if (mPhone334TextWatcher == null) {
                    mPhone334TextWatcher = new Phone334TextWatcher();
                }
                editText.addTextChangedListener(mPhone334TextWatcher);
            }
        }
    }

    private class Phone334TextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }


        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            PhoneFormat.onTextChanged344(editText, s);
        }
    }

}
